SpringMVC中@JsonSerialize通过注解格式化字段样式

您所在的位置:网站首页 jsonserialize注解 不生效 SpringMVC中@JsonSerialize通过注解格式化字段样式

SpringMVC中@JsonSerialize通过注解格式化字段样式

2024-07-11 11:03| 来源: 网络整理| 查看: 265

jackson-databind-x.x.x.jar

       在后台查询出来的数据后,有时候我们需要对数据的值进行格式化,比如日期,时间,若不加任何说明情况下Date类型将以时间戳的形式转换为Json并返回。

       jackson提供了一些自定义格式的方法。我们只需继承它的抽象类JsonSerializer ,并在指定的属性方法上添加注解@JsonSerialize即可实现。

/** * Annotation used for configuring serialization aspects, by attaching * to "getter" methods or fields, or to value classes. * When annotating value classes, configuration is used for instances * of the value class but can be overridden by more specific annotations * (ones that attach to methods or fields). *

* An example annotation would be: * * @JsonSerialize(using=MySerializer.class, * as=MySubClass.class, * typing=JsonSerialize.Typing.STATIC * ) * * (which would be redundant, since some properties block others: * specifically, 'using' has precedence over 'as', which has precedence * over 'typing' setting) */ @Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @com.fasterxml.jackson.annotation.JacksonAnnotation public @interface JsonSerialize ..........省略..............

翻译

用于配置序列化方面的注释,通过附加到“getter”方法或字段或值类。 注解值类时,配置用于值类的实例,但可以被更具体的注释(附加到方法或字段的注释)覆盖。

一个注解的例子:JsonSerialize(using=MySerializer.class, as=MySubClass.class,typing=JsonSerialize.Typing.STATIC)

这将是多余的,因为一些属性会阻止其他属性:具体而言,“using”优先于“as”,优先于“typing”设置。

JsonSerialize主要是后台传递给前台时的日期格式。

比如:1、保留两位小数  2、将日期格式化yy-MM-dd  3、时间格式化 yyyy-MM-dd HH:mm:ss

/** * 用户将double类型的数据格式化成小数点后两位的字符串数据:如输出为“900.00”. * @author Shine * */ public class CustomDoubleSerialize extends JsonSerializer { private DecimalFormat df = new DecimalFormat("#0.00"); public CustomDoubleSerialize() { } public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(this.df.format(value)); } }

 

/** * 时间序列化工具,直接将时间类型的转为yyyy-MM-dd类型的数据 * 使用方法:在实体的get方法上直接加@JsonSerialize(using = CustomDateSerialize.class) * @author Shine */ public class CustomDateSerialize extends JsonSerializer { //定义时间格式 private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); @Override public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(sdf.format(date)); } } public class JsonTimeStampSerializer extends JsonSerializer { private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void serialize(final Timestamp timestamp, final JsonGenerator gen, final SerializerProvider provider) throws IOException, JsonProcessingException { String value = dateFormat.format(timestamp); gen.writeString(value); } }

在自己的vo中就可以使用这样的注解

@Data public abstract class IBaseBo { /** 创建时间 */ @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonSerialize(using = JsonTimeStampSerializer.class) private Timestamp createTime; /** 修改人 */ private long modifyUser; /** 更新时间 */ @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonSerialize(using = JsonTimeStampSerializer.class) private Timestamp updateTime; /** 版本信息 */ private int version; }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3